Showing Which Sections Differences Are in

Contents|Index|Previous|Next

Showing Which Sections Differences Are in

Sometimes you might want to know which part of the files each change falls in. If the files are source code, this could mean which function was changed. If the files are documents, it could mean which chapter or appendix was changed. GNU diff can show this by displaying the nearest section heading line that precedes the differing lines. Which lines are "section headings" is determined by a regular expression.

Showing Lines That Match Regular Expressions

To show in which sections differences occur for files that are not source code for C or similar languages, use the '-F regexp' or '--show-function-line=regexp' option. diff considers lines that match the argument, regexp, to be the beginning of a section of the file. Here are suggested regular expressions for some common languages:

C, C++, Prolog
'ˆ[A-Za-z_]'

Lisp
'ˆ('

Texinfo
'ˆ@\(chapter\|appendix\|unnumbered\|chapheading\)'

This option does not automatically select an output format; in order to use it, you must select the context format (see Context Format) or unified format (see Unified Format). In other output formats it has no effect.

The '-F' and '--show-function-line' options find the nearest unchanged line that precedes each hunk of differences and matches the given regular expression. Then they add that line to the end of the line of asterisks in the context format, or to the '@@' line in unified format. If no matching line exists, they leave the output for that hunk unchanged. If that line is more than 40 characters long, they output only the first 40 characters. You can specify more than one regular expression for such lines; diff tries to match each line against each regular expression, starting with the last one given. This means that you can use '-p' and '-F' together, if you wish.

Showing C Function Headings

To show in which functions differences occur for C and similar languages, you can use the '-p' or '--show-c-function' option. This option automatically defaults to the context output format (see Context Format), with the default number of lines of context. You can override that number with '-C lines' elsewhere in the command line. You can override both the format and the number with '-U lines' elsewhere in the command line.

The '-p' and '--show-c-function' options are equivalent to '-F'ˆ[_a-zA-Z$]'' if the unified format is specified, otherwise '-c -F'ˆ[_a-zA-Z$]'' (see previous, Showing Lines That Match Regular Expressions).

GNU diff provides them for the sake of convenience.